//
//  CZWelcomeViewController.swift
//  weibo
//
//  Created by Apple on 16/3/11.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

//  欢迎页面
class CZWelcomeViewController: UIViewController {

    // MARK:    背景图片
    private lazy var bgImageView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "ad_background"))
        return imageView
    }()
    
    private lazy var headImageView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar_default"))
        
        //  如果图片存在，设置图片
        if let imageUrlStr = CZUserAccountViewModel.sharedAccount.userAccount?.avatar_large {
            let headImageUrl = NSURL(string: imageUrlStr)
            imageView.sd_setImageWithURL(headImageUrl, placeholderImage: UIImage(named: "avatar_default"))
        }
        return imageView
    }()
    
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.text = "欢迎回来"
        label.font = UIFont.systemFontOfSize(15)
        label.textColor = UIColor.darkGrayColor()
        
        return label
    }()
    
    override func loadView() {
        view = bgImageView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setUpUI()
        
    }
    
    //  执行动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        //  更新约束
        headImageView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(self.view).offset(100)
        }
    
        //  消息文本的透明度是0
        messageLabel.alpha = 0
        //        usingSpringWithDamping 阻尼 0 - 1
        //        initialSpringVelocity 执行动画的初始速度
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            
            self.view.layoutIfNeeded()
            
        }) { (_) -> Void in
                
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    
                    self.messageLabel.alpha = 1
                    
                    }, completion: { (_) -> Void in
                        
                        //  切换跟视图
                        NSNotificationCenter.defaultCenter().postNotificationName(CZSwitchRootVCNotifation, object: self)
                        
                })
                
                
        }
        
    }
    
    
    private func setUpUI() {
        view.addSubview(headImageView)
        view.addSubview(messageLabel)
        
        
        headImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.view).offset(200)
            make.centerX.equalTo(self.view.snp_centerX)
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImageView.snp_centerX)
            make.top.equalTo(headImageView.snp_bottom).offset(15)
            
        }
        //  设置圆角
        headImageView.layer.cornerRadius = 45
        headImageView.layer.masksToBounds = true
    
    }
    
    

    
}
